package base

import (
	"hi-jvm/rtda"
	"hi-jvm/rtda/heap"
)

// jvms 5.5
func InitClass(thread *rtda.Thread, class *heap.Class) {
	class.StartInit()
	scheduleClinit(thread, class)
	initSuperClass(thread, class)
}

func scheduleClinit(thread *rtda.Thread, class *heap.Class) {
	clinit := class.GetClinitMethod()
	if clinit == nil {
		return
	}

	// exec <clinit>
	newFrame := thread.NewFrame(clinit)
	thread.PushFrame(newFrame)
}

func initSuperClass(thread *rtda.Thread, class *heap.Class) {
	if class.IsInterface() {
		return
	}

	superClass := class.SuperClass
	if superClass != nil && !superClass.InitStarted {
		InitClass(thread, superClass)
	}
}

